package com.aegis.demo;

import com.aegis.core.enums.AegisHttpMethod;
import com.aegis.core.model.AegisUserApi;
import com.aegis.core.model.AegisAppGroupPermission;
import com.aegis.spring.AegisInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube 演示用户管理-初始化
 * 该demo是基于共享session，用于管理用户对你的服务的访问权限
 * 如果仅希望用aegis来限制外部系统对你的服务访问，不需要在你的服务配置任何内容，可跳过当前demo
 */
@Configuration
public class AegisConfig {


    /**
     * 这里是基于共享session（可访问同一个redis）的前提下，注册+初始化app
     *
     * @return
     */
    @Bean
    public AegisInitializer aegisInitializer() {
        //这里模拟服务查询权限组 + 权限组可以访问的api
        List<AegisAppGroupPermission> groupPermissions = new ArrayList<>();
        groupPermissions.add(getAdminPermissions());
        groupPermissions.add(getUserPermissions());
        groupPermissions.add(getManagePermissions());
        return new AegisInitializer()
                .initGroupPermission(groupPermissions);
    }

    /**
     * 超管权限
     *
     * @return
     */
    public AegisAppGroupPermission getAdminPermissions() {
        AegisAppGroupPermission groupPermission = new AegisAppGroupPermission();
        groupPermission.setGroupId("1");
        groupPermission.setGroupKey("admin");
        groupPermission.setGroupName("超级管理员");
        List<AegisUserApi> apis = new ArrayList<>();
        AegisUserApi aegisUserApi = new AegisUserApi();
        aegisUserApi.setApi("/**");
        aegisUserApi.setName("超管全接口权限");
        aegisUserApi.setMethod(AegisHttpMethod.ALL);
        apis.add(aegisUserApi);
        groupPermission.setApis(apis);
        return groupPermission;
    }


    /**
     * 普通用户权限
     *
     * @return
     */
    public AegisAppGroupPermission getUserPermissions() {
        AegisAppGroupPermission groupPermission = new AegisAppGroupPermission();
        groupPermission.setGroupId("2");
        groupPermission.setGroupKey("user");
        groupPermission.setGroupName("普通用户");
        List<AegisUserApi> apis = new ArrayList<>();

        AegisUserApi aegisUserApi1 = new AegisUserApi();
        //或者/user/me/**也可以
        aegisUserApi1.setApi("/demo1/auth/user/me/profile");
        aegisUserApi1.setName("用户个人信息");
        aegisUserApi1.setMethod(AegisHttpMethod.GET);
        apis.add(aegisUserApi1);

        groupPermission.setApis(apis);
        return groupPermission;
    }


    /**
     * 普通管理员权限
     *
     * @return
     */
    public AegisAppGroupPermission getManagePermissions() {
        AegisAppGroupPermission groupPermission = new AegisAppGroupPermission();
        groupPermission.setGroupId("3");
        groupPermission.setGroupKey("manage");
        groupPermission.setGroupName("普通管理员");

        List<AegisUserApi> apis = new ArrayList<>();
        AegisUserApi aegisUserApi1 = new AegisUserApi();
        aegisUserApi1.setApi("/demo1/auth/user/me/profile");
        aegisUserApi1.setName("用户个人信息");
        aegisUserApi1.setMethod(AegisHttpMethod.GET);
        apis.add(aegisUserApi1);

        AegisUserApi aegisUserApi2 = new AegisUserApi();
        aegisUserApi2.setApi("/demo1/auth/user/*/profile");
        aegisUserApi2.setName("查询用户信息");
        aegisUserApi2.setMethod(AegisHttpMethod.GET);
        apis.add(aegisUserApi2);

        groupPermission.setApis(apis);
        return groupPermission;
    }



}
